﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Newtonsoft.Json;

namespace CSharpLearningWeb.Service
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AjaxService
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }

        // Add more operations here and mark them with [OperationContract]
        [OperationContract]
        public string GetDate()
        {
            return DateTime.Now.ToString("dd/MMM/yyyy HH:mm:ss");
        }

        [OperationContract]
        public string HelloWorld(string personName)
        {
            var msg = (new MessageWrapper()
            {
                Response = string.Format("Hello, {0}", personName),
                Error = string.Empty,
                TimeExecuted = DateTime.Now.ToString()
            });

            return JsonConvert.SerializeObject(msg, Formatting.Indented);
        }

        [OperationContract]
        public string GetEmployee()
        {
            List<Employee> eList = new List<Employee>();
            Employee e = new Employee();
            e.Name = "Minal";
            e.Age = 24;

            eList.Add(e);

            e = new Employee();
            e.Name = "Santosh";
            e.Age = 24;

            eList.Add(e);

            string ans = JsonConvert.SerializeObject(eList, Formatting.Indented);
            return ans;           
        }

        public class Employee
        {
            public string Name;
            public int Age;
        }

        public class MessageWrapper
        {
            public string Response;
            public string TimeExecuted;
            public string Error;
        }
    }
}
